<?php
/**
 * Created by PhpStorm.
 * User: Lyq
 * Date: 2018/8/22
 * Time: 上午8:21
 */
namespace app\bussiness\controller;

use think\Exception;
use think\Db;

class Bussiness extends Common
{
    //编辑商会
    public function index()
    {
        //商会ID
        $articleId = $this->currentBussinessId();

        if (request()->isGet())
        {   $this->templateTitle('商会信息');
            //获取新闻信息
            $article = Db::name("bussiness")
                ->where("id", $articleId)
                ->find();
            $this->assign("article", $article);

            //处理缩略图
            $this->assign("single_image", array("thumb"=>$article["avatar"]));
            //处理详细内容
            $this->assign("ueditor_content", array("article_detail"=>$article["detail"]));
            $this->assign("area_province", $article['pro']);
            $this->assign("area_city", $article['city']);

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            //分类ID
            $data['pro'] = request()->param("pro");
            if(empty($data['pro'])){
                return $this->ajaxError('请选择省份');
            }
            $data['city'] = request()->param("city");
            if(empty($data['city'])){
                return $this->ajaxError('请选择城市');
            }



            //新闻名称
            $data["title"] = request()->param("title");
            if(empty($data['title'])){
                return $this->ajaxError('请输入商会名称');
            }
            $count = Db::name("bussiness")
                ->where('id', 'NEQ', $articleId)
                ->where("title", $data['title'])
                ->count();
            if ($count)
            {
                $this->ajaxError("【" . $data['title'] . "】" . '商会名称已存在');
            }
            //新闻缩略图
            $data["avatar"] = request()->param("thumb");
            if(empty($data['avatar'])){
                return $this->ajaxError('请上传logo');
            }

            //联系电话
            $data["tel"] = request()->param("tel");
            //详细地址
            $data["address"] = request()->param("address");

            //简介
            $detail = request()->param("article_detail");
            $data["detail"] = $detail;


            try
            {
                $save = Db::name("bussiness")
                    ->where("id", $articleId)
                    ->update($data);
                if($save){
                    //商会城市切换同时更新新闻
                    $change['pro'] = $data['pro'];
                    $change['city'] = $data['city'];
                    Db::name('content_article')
                        ->where("bussiness_id", $articleId)
                        ->update($change);

                    $this->ajaxSuccess('商会信息编辑成功');
                }else{
                    $this->ajaxError('商会信息编辑失败');
                }
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

}